package ru.mera.aaclog.db;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

/**
 * @version 1.0 2/03/2013
 * @author anavdeev
 */
@Entity
@Table(name="messages")
@NamedQueries( {
    @NamedQuery(name = MessageHeader.GET_ALL, 
                query = "SELECT m FROM MessageHeader AS m"),
    @NamedQuery(name = MessageHeader.GET_BY_LOG, 
                query = "SELECT m FROM MessageHeader AS m WHERE m.log = ?1"),
    @NamedQuery(name = MessageHeader.GET_BY_ID, 
                query = "SELECT m FROM MessageHeader AS m WHERE m.id = ?1"),
    @NamedQuery(name = MessageHeader.GET_BY_PATTERN, 
                query = "SELECT m FROM  MessageHeader AS m, MessageBody AS mb WHERE m.log = ?1 AND mb.message=m.id AND mb.value LIKE ?2 ")
} )
public class MessageHeader implements Serializable {

    public static final String GET_ALL = "getAllMsgs";
    public static final String GET_BY_ID = "getMsg";
    public static final String GET_BY_LOG = "getMsgsByLog";
    public static final String GET_BY_PATTERN = "getMsgsByPattern";

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "mess_id")
    private int id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "mess_log", nullable = false)
    private LogHeader log;

    @OneToMany(mappedBy="message")
    @OnDelete(action=OnDeleteAction.CASCADE)
    private Set<MessageBody> messageRaw;

    @OneToMany(mappedBy="message")
    @OnDelete(action=OnDeleteAction.CASCADE)
    private Set<MessageXmlBody> messageXml;

    @Column(name = "mess_type", columnDefinition="")
    private String sipMessageType;
    @Column(name = "mess_transaction", columnDefinition="")
    private String trans;
    @Column(name = "mess_from", columnDefinition="")
    private String from;
    @Column(name = "mess_from_tag", columnDefinition="")
    private String fromTag;
    @Column(name = "mess_to", columnDefinition="")
    private String to;
    @Column(name = "mess_to_tag", columnDefinition="")
    private String toTag;
    @Column(name = "mess_cseq", columnDefinition="")
    private String cSeq;
    @Column(name = "mess_call_id", columnDefinition="")
    private String callId;
    @Column(name = "mess_context_length", columnDefinition="")
    private String len;
    @Column(name = "mess_context_type", columnDefinition="")
    private String type;
    @Column(name = "mess_contact", columnDefinition="")
    private String contact;
    @Column(name = "mess_agent", columnDefinition="")
    private String userAgent;

    public MessageHeader() {
        // Serializable ctor
    }

    public MessageHeader(Integer id) {
        this.id = id;
    }
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    public LogHeader getLog() {
        return log;
    }

    public void setLog(LogHeader log) {
        this.log = log;
    }

    public Set<MessageBody> getMessageRaw() {
        return messageRaw;
    }

    public void setMessageRaw(Set<MessageBody> messageRaw) {
        this.messageRaw = messageRaw;
    }

    public Set<MessageXmlBody> getMessageXml() {
        return messageXml;
    }

    public void setMessageXml(Set<MessageXmlBody> messageXml) {
        this.messageXml = messageXml;
    }

    public String getSipMessageType() {
        return sipMessageType;
    }

    public void setSipMessageType(String sipMessageType) {
        this.sipMessageType = sipMessageType;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getFromTag() {
        return fromTag;
    }

    public void setFromTag(String fromTag) {
        this.fromTag = fromTag;
    }

    public String getToTag() {
        return toTag;
    }

    public void setToTag(String toTag) {
        this.toTag = toTag;
    }

    public String getcSeq() {
        return cSeq;
    }

    public void setcSeq(String cSeq) {
        this.cSeq = cSeq;
    }

    public String getCallId() {
        return callId;
    }

    public void setCallId(String callId) {
        this.callId = callId;
    }

    public String getLen() {
        return len;
    }

    public void setLen(String len) {
        this.len = len;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getTrans() {
        return trans;
    }

    public void setTrans(String trans) {
        this.trans = trans;
    }

    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }
    
}
